package com.kgc.javaweb.dao.impl;

import cn.hutool.json.JSONUtil;

import com.mysql.jdbc.StringUtils;
import com.kgc.javaweb.dao.HouseDao;
import com.kgc.javaweb.dto.UserPageDTO;
import com.kgc.javaweb.entity.House;
import com.kgc.javaweb.enums.CodeMsg;
import com.kgc.javaweb.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: mosin
 * @version: v1.0  2022/11/9
 */
public class HouseDaoImpl implements HouseDao {

    @Override//用、用、用
    public List<House> selectPage(UserPageDTO userPageDTO) {

        Integer page = userPageDTO.getPage();
        Integer limit = userPageDTO.getLimit();

        Integer start = (page - 1) * limit;


        String searchParams = userPageDTO.getSearchParams();

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql = "select *  from house  where 1=1 ";

        ArrayList<Object> params = new ArrayList<>();

        // 搜索分页
        if (searchParams != null) {
            //  username  address
            House house = JSONUtil.toBean(searchParams, House.class);
            String roomname = house.getRoomname() ;
            String housetype = house.getHousetype();
            String belongunit = house.getBelongunit();
            String buildname = house.getBuildname();

            if(!StringUtils.isEmptyOrWhitespaceOnly(roomname)) {
                sql += " and roomname = ?";
                params.add(roomname);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(housetype)) {
                sql += " and housetype = ?";
                params.add(housetype);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(belongunit)) {
                sql += " and belongunit = ?";
                params.add(belongunit);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(buildname)) {
                sql += " and buildname = ?";
                params.add(buildname);
            }
        }
        sql += " limit ? , ?";
        System.out.println("sql = " + sql);
        params.add(start);
        params.add(limit);
        System.out.println("params = " + params);

        try {
            List<House> houses = queryRunner.query(sql, new BeanListHandler<House>(House.class), params.toArray());
            return houses;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("分页查询失败");
        }
    }

    @Override//用、用、用
    public Long selectTotal(UserPageDTO userPageDTO) {

        //查询总条数   搜索总条数
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());

        String sql = "select count(*) from house where 1=1 ";

        String searchParams = userPageDTO.getSearchParams();
        ArrayList<Object> params = new ArrayList<>();

        if (searchParams != null) {

            House house = JSONUtil.toBean(searchParams, House.class);
            String roomname = house.getRoomname() ;
            String housetype = house.getHousetype();
            String belongunit = house.getBelongunit();
            String buildname = house.getBuildname();


            if(!StringUtils.isEmptyOrWhitespaceOnly(roomname)){
                sql+=" and roomname = ?";
                params.add(roomname);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(housetype)) {
                sql += " and housetype = ?";
                params.add(housetype);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(belongunit)) {
                sql += " and belongunit = ?";
                params.add(belongunit);
            }
            if (!StringUtils.isEmptyOrWhitespaceOnly(buildname)) {
                sql += " and buildname = ?";
                params.add(buildname);
            }
        }

        try {
            Long total = queryRunner.query(sql, new ScalarHandler<Long>(), params.toArray());
            return total;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("查询总条数失败");
        }
    }

    @Override//用、用、用
    public House selectById(Integer id) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "select *  from house where id = ?";
        try {
            House house = queryRunner.query(sql, new BeanHandler<House>(House.class), id);
            return house;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException("查询数据失败！");
        }
    }

    @Override//用、用、用
    public void update(House hous, Integer id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "update house set roomname = ? ,housearea = ?,housetype = ?,belongunit = ?,buildname = ? where id = ?";

        try {
            queryRunner.update(sql, hous.getRoomname(), hous.getHousearea(), hous.getHousetype(), hous.getBelongunit(), hous.getBuildname(), id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(CodeMsg.UPDATE_FAIL.getMsg());
        }

    }

    @Override//用、用、用
    public void add(House hous) {

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "insert into house (roomname,housearea,housetype,belongunit,buildname)values(?,?,?,?,?)";
        try {
            int update = queryRunner.update(sql, hous.getRoomname(), hous.getHousearea(), hous.getHousetype(), hous.getBelongunit(), hous.getBuildname());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new RuntimeException(CodeMsg.ADD_FAIL.getMsg());
        }
    }

    @Override//用、用、用
    public void delete(String ids) {  // 1,2,3  1

        QueryRunner queryRunner = new QueryRunner(DruidUtil.getDataSource());
        String sql = "delete from house where id = ?";

        String[] split = ids.split(",");
        ArrayList<Integer> allId = new ArrayList<>();
        for (String s : split) {
            allId.add(Integer.valueOf(s));
        }

        allId.forEach(id -> {
            try {
                queryRunner.update(sql, id);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                throw new RuntimeException(CodeMsg.DELETE_FAIL.getMsg());
            }
        });
    }


}
